<?php

class ItemController extends \BaseController {


    public function viewCheckout()
    {
        return View::make('checkout')->with(array ('title'=>'checkout'));
    }

    public function storeCheckout()
    {
        $validator = Validator::make(
            Input::all(),
            array (
                'item_number' => 'required|numeric|itemnumbervalid|itemnumberinstock',
                'checked_out_at' => 'date|after:1950-01-01',
                'status' => 'required'
            )
        );

        if ($validator->fails()) {
            return Redirect::to('/checkout')->withErrors($validator)->withInput();       
        } else {
            $item = Item::where('item_number', '=', Input::get('item_number'))->first();
            $item->checked_out_at = date('Y-m-d', strtotime(Input::get('checked_out_at')));
            $item->status = Input::get('status');
            $item->save();
        }
        
        Session::flash('message', 'Successfully checked out');
        return Redirect::to('/lots');
        
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $group_id = Auth::user()->group_id;
        $items = Item::where('group_id', '=', $group_id)
            ->where('out_stock_at','=','0000-00-00:00:00:00')
            ->get();

        return View::make('items.index', array ('items'=>$items, 'title'=>'items'));
    }
    
    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('items.create')->with('title', 'create item');
    }
    
    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $validator = Validator::make(
            Input::all(),
            array ('name' => 'required')
        );

        if ($validator->fails()) {
            return Redirect::to('items/create')->withErrors($validator)->withInput();       
        } else {
            $item = new Item;
            $item->name = Input::get('name');
            $item->group_id = Auth::user()->group_id;
            $item->save();
        }
        
        Session::flash('message', 'Successfully created item ' . $item->id);
        return Redirect::to('items');
    }
    
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $item = Item::find($id);
        return View::make('items.show')->with(array ('item'=>$item, 'title'=>'item'));
    }
    
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }
    
    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //
    }
    
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
    
}